package com.CTAtracker.android.Fragments;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.Activities.CTAMapActivity;
import com.CTAtracker.android.Activities.SlidingActivity;
import com.CTAtracker.android.Adapters.SlidingMenuItemAdapter;

/**
 * Fragment regarding the sliding function
 */
public class SlidingMenuFragment extends Fragment {
	/**
	 * The List of the function and their key
	 */
	private ArrayList<Pair<String, Integer>> _listRow;
	/**
	 * The sliding activity
	 */
	private SlidingActivity _slidingActivity;
	/**
	 * The view of the list of function
	 */
	private ListView _listView;
	/**
	 * Log
	 */
	private static final String TAG = "SlidingMenuFragment";
	
	@Override
	public void onAttach(Activity act) {
		super.onAttach(act);
		_slidingActivity = (SlidingActivity) act;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.sliding_menu_content, container, false);
	}

	@Override
	public void onStart(){
		super.onStart();

		_listView = (ListView) getView().findViewById(R.id.sliding_menu_listView);
		_listRow = new ArrayList<Pair<String, Integer>>();
		_listRow.add(new Pair<String, Integer>("Line", R.drawable.line_icon));
		_listRow.add(new Pair<String, Integer>("Search", R.drawable.search_icon));
		_listRow.add(new Pair<String, Integer>("Around", R.drawable.around_icon));
		_listRow.add(new Pair<String, Integer>("Favorites", R.drawable.is_not_favorite));
		_listRow.add(new Pair<String, Integer>("Map", R.drawable.maps_icon));
		_listRow.add(new Pair<String, Integer>("About", R.drawable.about_icon));


		SlidingMenuItemAdapter adapter = new SlidingMenuItemAdapter(getActivity(), R.layout.sliding_menu_row, _listRow);
		_listView.setAdapter(adapter);

		_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				switch (position) {
				case 0:
					_slidingActivity.replaceContent(new SearchLineFragment());
					break;
				case 1: 
					_slidingActivity.replaceContent(new SearchFragment());
					break;
				case 2:
					_slidingActivity.replaceContent(new AroundFragment());
					break;
				case 3:
					_slidingActivity.replaceContent(new FavoritesFragment());
					break;
				case 4:
					Log.v(TAG, "Creation of the map intent");
					Intent intent = new Intent((SlidingActivity)getActivity(), CTAMapActivity.class);
					startActivity(intent);
					
					break;
				case 5:
					_slidingActivity.replaceContent(new AboutFragment());
					break;
				default:
				}

				_slidingActivity.toggleSlidingMenu();
			}
		});
	}
}
